package com.group01.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.group01.community.entity.SysUser;
import com.group01.community.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 用户查询实现类（自定义登录拦截）
 */
@Service
public class SysUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserMapper sysUserMapper;

    //返回用户详情信息
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("user_name", username).eq("status", 0));
        if(sysUser == null){
           throw new UsernameNotFoundException("用户不存在");
       }
        org.springframework.security.core.userdetails.User user = new org.springframework.security.core.userdetails.User(username, sysUser.getPassword()
                , AuthorityUtils.commaSeparatedStringToAuthorityList(""));
        System.out.println(user);
        return user;
    }
}
